﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ltd
{
    [HotUpdate]
    [Description("采购申请单表单插件")]
    public class PurchaseRequest: AbstractDynamicFormPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if(e.Field.Key.Equals("FMaterialId"))
            {
                string FMaterialId = "";
                DateTime FAPPLICATIONDATE;
                DateTime FAPPLICATIONDATE7;
                DateTime FAPPLICATIONDATE14;
                DateTime FAPPLICATIONDATE30;
                int rowIndex;
                decimal FREQQTY7 = 0;decimal FREQQTY14 = 0;decimal FREQQTY30 = 0;decimal F_TRAV_AMOUNT_QTR2 = 0;decimal FREMAINSTOCKINQTY=0;
                decimal FTAXPRICE30 = 0;decimal FTAXPRICEmix = 0;decimal FREMAINRECEIVEQTY = 0;
                if (this.Model.GetValue("FAPPLICATIONDATE") != null)
                {
                    FAPPLICATIONDATE = Convert.ToDateTime(this.Model.GetValue("FAPPLICATIONDATE"));
                    FAPPLICATIONDATE7 = FAPPLICATIONDATE.AddDays(-7);
                    FAPPLICATIONDATE14 = FAPPLICATIONDATE.AddDays(-14);
                    FAPPLICATIONDATE30 = FAPPLICATIONDATE.AddDays(-30);
                    rowIndex = this.Model.GetEntryCurrentRowIndex("FEntity");
                    if (this.Model.GetValue("FMaterialId", rowIndex) != null)
                    {
                        FMaterialId = Convert.ToString((this.Model.GetValue("FMaterialId", rowIndex) as DynamicObject)["Id"]);
                        //获取近7天日均销量
                        string SQL = "/*dialect*/" + string.Format(@"select sum(t1.FPRICEQTY)/7 FREQQTY7 from t_AR_receivable t
                        left join t_AR_receivableEntry t1 on t.FID=t1.FID
                        WHERE T.FDATE>='{0}'  and t1.FMaterialId='{1}' and t.FDOCUMENTSTATUS='C'
                        ", FAPPLICATIONDATE7, FMaterialId);
                        DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                        if(DynamData.Count>0)
                        {
                            FREQQTY7 = Convert.ToDecimal(DynamData[0]["FREQQTY7"]);
                            this.Model.SetValue("F_TRAV_Qty_ca9", FREQQTY7, rowIndex);
                        }
                        //获取近14天日均销量
                        string SQL1 = "/*dialect*/" + string.Format(@"SELECT sum(T1.FPRICEQTY)/14 FREQQTY14 FROM t_AR_receivable T
                        LEFT JOIN t_AR_receivableEntry T1 ON T.FID=T1.FID
                        WHERE T.FDATE>='{0}'  and t1.FMaterialId='{1}' and t.FDOCUMENTSTATUS='C'
                        ", FAPPLICATIONDATE14, FMaterialId);
                        DynamicObjectCollection DynamData1 = DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                        if (DynamData1.Count > 0)
                        {
                            FREQQTY14 = Convert.ToDecimal(DynamData1[0]["FREQQTY14"]);
                            this.Model.SetValue("F_TRAV_Qty_uky", FREQQTY14, rowIndex);
                        }
                        //获取近30天日均销量
                        string SQL2 = "/*dialect*/" + string.Format(@"SELECT sum(T1.FPRICEQTY)/30 FREQQTY30 FROM t_AR_receivable T
                        LEFT JOIN t_AR_receivableEntry T1 ON T.FID=T1.FID
                        WHERE T.FDATE>='{0}'  and t1.FMaterialId='{1}' and t.FDOCUMENTSTATUS='C'
                        ", FAPPLICATIONDATE30, FMaterialId);
                        DynamicObjectCollection DynamData12 = DBUtils.ExecuteDynamicObject(this.Context, SQL2);
                        if (DynamData12.Count > 0)
                        {
                            FREQQTY30 = Convert.ToDecimal(DynamData12[0]["FREQQTY30"]);
                            this.Model.SetValue("F_TRAV_Qty_dvn", FREQQTY30, rowIndex);
                        }
                        //获取最近组柜平均到仓价
                        string SQL3 = "/*dialect*/" + string.Format(@"SELECT T1.FMATERIALID,T.FDATE,T1.F_TRAV_AMOUNT_QTR2 FROM T_PUR_Receive T
                        LEFT JOIN T_PUR_ReceiveEntry T1 ON T.FID=T1.FID
                        INNER JOIN (SELECT A1.FMATERIALID,MAX(A.FDATE)NEWFDATE FROM T_PUR_Receive A
                        LEFT JOIN T_PUR_ReceiveEntry A1 ON A.FID=A1.FID
                        GROUP BY A1.FMATERIALID)T2 ON T1.FMATERIALID=T2.FMATERIALID AND T.FDATE=T2.NEWFDATE
                        WHERE T1.FMaterialId='{0}' and t.FDOCUMENTSTATUS='C'
                        ", FMaterialId);
                        DynamicObjectCollection DynamData13 = DBUtils.ExecuteDynamicObject(this.Context, SQL3);
                        if (DynamData13.Count > 0)
                        {
                            F_TRAV_AMOUNT_QTR2 = Convert.ToDecimal(DynamData13[0]["F_TRAV_AMOUNT_QTR2"]);
                            this.Model.SetValue("F_TRAV_Price_w5c", F_TRAV_AMOUNT_QTR2, rowIndex);
                            this.Model.SetValue("F_TRAV_Price_bgx", F_TRAV_AMOUNT_QTR2, rowIndex);
                        }
                        //获取采购在途数量
                        string SQL4 = "/*dialect*/" + string.Format(@"SELECT (SUM(T3.FPRICEQTY)-SUM(T2.FINSTOCKQTY))FREMAINSTOCKINQTY FROM T_PUR_Receive T
                        LEFT JOIN T_PUR_ReceiveEntry T1 ON T.FID=T1.FID
                        LEFT JOIN T_PUR_RECEIVEENTRY_S T2 ON T1.FENTRYID=T2.FENTRYID
						LEFT JOIN T_PUR_RECEIVEENTRY_F T3 ON T1.FENTRYID=T3.FENTRYID
                        WHERE T1.FMaterialId='{0}' and t.FDOCUMENTSTATUS='C'
                        ", FMaterialId);
                        DynamicObjectCollection DynamData14 = DBUtils.ExecuteDynamicObject(this.Context, SQL4);
                        if (DynamData14.Count > 0)
                        {
                            FREMAINSTOCKINQTY = Convert.ToDecimal(DynamData14[0]["FREMAINSTOCKINQTY"]);
                            this.Model.SetValue("F_TRAV_Qty_fg2", FREMAINSTOCKINQTY, rowIndex);
                        }
                        //获取近30天日均单价
                        string SQL5 = "/*dialect*/" + string.Format(@"SELECT (sum(t1.FALLAMOUNT)/sum(t1.FPRICEQTY)) FTAXPRICE30 FROM t_AR_receivable T
                        LEFT JOIN t_AR_receivableEntry T1 ON T.FID=T1.FID
                        WHERE T.FDATE>='{0}'  and t1.FMaterialId='{1}' and t.FDOCUMENTSTATUS='C'
                        ", FAPPLICATIONDATE30, FMaterialId);
                        DynamicObjectCollection DynamData15 = DBUtils.ExecuteDynamicObject(this.Context, SQL5);
                        if (DynamData15.Count > 0)
                        {
                            FTAXPRICE30 = Convert.ToDecimal(DynamData15[0]["FTAXPRICE30"]);
                            this.Model.SetValue("F_TRAV_PRICE_F74", FTAXPRICE30, rowIndex);
                        }
                        //获取历史最低价
                        string SQL6 = "/*dialect*/" + string.Format(@"SELECT min(t1.FTAXPRICE) FTAXPRICEmix FROM t_AR_receivable T
                        LEFT JOIN t_AR_receivableEntry T1 ON T.FID=T1.FID
                        WHERE t1.FMaterialId='{0}' and t.FDOCUMENTSTATUS='C' and t1.FTAXPRICE<>0
                        ", FMaterialId);
                        DynamicObjectCollection DynamData16 = DBUtils.ExecuteDynamicObject(this.Context, SQL6);
                        if (DynamData16.Count > 0)
                        {
                            FTAXPRICEmix = Convert.ToDecimal(DynamData16[0]["FTAXPRICEmix"]);
                            this.Model.SetValue("F_TRAV_Price_yiu", FTAXPRICEmix, rowIndex);
                        }
                        //获取未发货数量
                        string SQL7 = "/*dialect*/" + string.Format(@"SELECT SUM(T2.FREMAINRECEIVEQTY)FREMAINRECEIVEQTY FROM t_PUR_POOrder T
						LEFT JOIN t_PUR_POOrderEntry T1 ON T.FID=T1.FID
						LEFT JOIN T_PUR_POORDERENTRY_R T2 ON T1.FENTRYID=T2.FENTRYID
                        WHERE t1.FMaterialId='{0}' and t.FDOCUMENTSTATUS='C'
                        ", FMaterialId);
                        DynamicObjectCollection DynamData17 = DBUtils.ExecuteDynamicObject(this.Context, SQL7);
                        if (DynamData17.Count > 0)
                        {
                            FREMAINRECEIVEQTY = Convert.ToDecimal(DynamData17[0]["FREMAINRECEIVEQTY"]);
                            this.Model.SetValue("F_TRAV_Qty_yrr", FREMAINRECEIVEQTY, rowIndex);
                        }
                    }
                }
                this.View.UpdateView("F_TRAV_Qty_ca9");
                this.View.UpdateView("F_TRAV_Qty_uky");
                this.View.UpdateView("F_TRAV_Qty_dvn");
                this.View.UpdateView("F_TRAV_Price_w5c");
                this.View.UpdateView("F_TRAV_Qty_fg2");
                this.View.UpdateView("F_TRAV_Price_bgx");
                this.View.UpdateView("F_TRAV_PRICE_F74");
                this.View.UpdateView("F_TRAV_Price_yiu");
                this.View.UpdateView("F_TRAV_Qty_yrr");
            }
        }
    }
}
